//
//  DataBaseManger.m
//  ui豆瓣
//
//  Created by lirui on 16/1/7.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "DataBaseManger.h"

@implementation DataBaseManger

+(DataBaseManger *)shareInstance
{ //静态指针变量 保证指针只有一个
    static DataBaseManger *dbManager = nil;
    //是否是第一次调用该方法
    //如果指针为空 创建对象
    if (!dbManager) {
        //单例中不需要release
        dbManager =[[ DataBaseManger alloc] init];
        
    }
    //如果指针不为空直接返回
    return dbManager;
}
-(void)openDB
{
    //数据库一般放在document
    //后缀 db/ sqlite
    NSString *path =[ NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"suibian.db"];
    //创建数据库文件
    //判断dbpoint指针中 是否存在数据库 如果不存在 在创建
    //参数1: 数据库路径(UTF-8转码)
    //参数2: 数据库指针地址
    int result =  sqlite3_open(path.UTF8String, &dbPoint);
    NSLog(@"打开数据库:%d, 路径: %@", result, path);
}

-(void)closeDB
{
    sqlite3_close(dbPoint);
    
}
-(void)createTable
{
    NSString *createSQL = @"CREATE TABLE IF NOT EXISTS dictionary(title text,rating text)";
    int result = sqlite3_exec(dbPoint, createSQL.UTF8String, NULL, NULL, NULL);
    //3.判断操作
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    }
    else {
        NSLog(@"失败:%d",result);
    }
    
}

-(void)dropTable
{
    //1.sql语句
    NSString *dropSQL = @"DROP TABLE dictionary";
    //2.执行
    int result = sqlite3_exec(dbPoint, dropSQL.UTF8String, NULL, NULL, NULL);
    //3.判断
    [self judgeResult:result message:@"删除表"];
}

-(void)insertMovie:(NSMutableDictionary *)m
{
    //1.sql语句
    NSString *insertSQL =[NSString stringWithFormat: @"INSERT INTO dictionary (title, rating) values('%@','%@')",m[@"title"], m[@"rating"]];
    //2.执行
    int result = sqlite3_exec(dbPoint, insertSQL.UTF8String, NULL, NULL, NULL);
    
    //3.判断
    
    [self judgeResult:result message:@"插入数据"];
    
    
    
}

//查询
-(NSMutableArray *)select
{//查询数组 数据处理过程
    //1.获取到所有数据
    //1.先遍历行 得到每一条(行)数据
    //2.在遍历当前行的 每一列 按照对应关系保存在model中
    //4.model添加到数组中 返回
    //1.SQL语句
    NSString *selectSQL =@"SELECT *FROM dictionary";
    //2.创建数据库替身
    sqlite3_stmt *stmt = nil;
    //3.执行
    int result = sqlite3_prepare_v2(dbPoint, selectSQL.UTF8String, -1, &stmt, nil);
    //4.初始化返回的数组
    NSMutableArray *arr =[ NSMutableArray array];
    //5.判断准备是否成功
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        //6.遍历每一行数据
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //逐行获取每一列信息
            //列数 从0开始
            
            const unsigned char *title = sqlite3_column_text(stmt, 0);
            const unsigned char *rating =sqlite3_column_text(stmt, 1);
           // int mId = sqlite3_column_int(stmt, 2);
            
            //创建model
          
            NSMutableDictionary *dic =[ NSMutableDictionary dictionary];
           dic[@"title"]=[ NSString stringWithUTF8String:(const char *)title];
           dic[@"rating"] =[ NSString stringWithUTF8String:(const char *) rating];
            
            NSLog(@"%@,%@",dic[@"title"], dic[@"rating"]);
           //dic[@"movieId"] = [mId ;
            //添加数组
            [arr addObject:dic];
        }
    }
    else
    {
        NSLog(@"失败:%d",result);
    }
    //返回数据
    return arr;
}

-(void)judgeResult:(int)r message:(NSString *)mes
{
    if (r == SQLITE_OK) {
        NSLog(@"%@成功", mes);
    }
    else
    {
        NSLog(@"%@失败: %d", mes, r);
    }
    
}

@end
